/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Valentina
 */
public class Pagination {

    public static void pagination(HttpServletRequest request, int pageNumber, int pageSize, int maxSizeList, String path) {

        int nextPage = (pageNumber + 1);
        int prevPage = (pageNumber - 1);

        if (prevPage != 0) {
            request.setAttribute("prevLink", path+"?page=" + prevPage);
        }
        int check = (nextPage - 1) * pageSize;

        if (check < maxSizeList) {
            request.setAttribute("nextLink", path+"?page=" + nextPage);
        }

        request.setAttribute("pageNumber", pageSize * prevPage);
    }

    public static int setPageNumber(HttpServletRequest request, int pageNumber) {
        if (request.getParameter("page") != null) {
            request.setAttribute("page", request.getParameter("page"));
            pageNumber = Integer.parseInt(request.getParameter("page"));
        } else {
            request.setAttribute("page", "1");
        }

        return pageNumber;
    }
}
